/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import PictureID.GridPictureID;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

/**
 *
 * @author corey
 */
public class FloorPanel extends JPanel {
    private static final short ELEVATOR_WIDTH = 100;
    private static final short ELEVATOR_HEIGHT = 200;
    private static final byte BORDER = 60;
    private static final byte MARGIN = 20; 
    private static final byte SELECT_WIDTH = 100;
    private static final byte BUTTON_HEIGHT = 100;
    private static final byte BUTTON_WIDTH = 50;
    private static final short DOOR_PANEL_WIDTH = 680;
    
    
    private short floorIdx;
    
    private DirectionButton upButton;
    private DirectionButton downButton;
    
    private Elevator[] elevators;
    private Floor[] floors;
    
    private FloorDoorPanel doorPanel;

    GridPictureID pictureIDs;
    
    int border = 12;
    
    public FloorPanel(Elevator[] e, Floor[] f) throws IOException{
        elevators = e;
        floors = f;
        floorIdx = 0;
        doorPanel = new FloorDoorPanel(elevators, floors);
        try {
            upButton = new DirectionButton(floors, true);
            downButton = new DirectionButton(floors, false);  
        } catch (IOException ex) {
            Logger.getLogger(FloorPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.add(upButton);
        buttonPanel.add(downButton);
        buttonPanel.setPreferredSize(new Dimension(50,100));
        buttonPanel.setBounds(SELECT_WIDTH+DOOR_PANEL_WIDTH-BUTTON_WIDTH-MARGIN, MARGIN+ELEVATOR_HEIGHT/2-BUTTON_HEIGHT/2, BUTTON_WIDTH, BUTTON_WIDTH*2);

        JScrollPane doorScroll = new JScrollPane(doorPanel);
        doorScroll.setBounds(SELECT_WIDTH, 0, DOOR_PANEL_WIDTH, ELEVATOR_HEIGHT+BORDER+MARGIN);
        
        ButtonGroup floorBG = new ButtonGroup();
        JRadioButton[] floorOptions = new JRadioButton[floors.length];
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
        radioPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Floor"),
            BorderFactory.createEmptyBorder(5,5,5,5)));
        for (short i = 0; i < floors.length; i++){
            final short j = i;
            floorOptions[i] = new JRadioButton(String.valueOf(i));
            if (i == 0){
                floorOptions[i].setSelected(true);
            }
            floorOptions[i].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                    floorIdx = j;
                }
            });
            floorBG.add(floorOptions[i]);
            radioPanel.add(floorOptions[i]);
        }
        
        pictureIDs = new GridPictureID(floors[floorIdx].getPeople());
        
        radioPanel.setPreferredSize(new Dimension(80, 50+23*floors.length));
        JScrollPane radioScroll = new JScrollPane(radioPanel);
        radioScroll.setBounds(0, 0, SELECT_WIDTH, 530);
        pictureIDs.setBounds(SELECT_WIDTH, ELEVATOR_HEIGHT+BORDER+MARGIN, pictureIDs.getPreferredSize().width, pictureIDs.getPreferredSize().height);
        this.setLayout(null);
        this.add(radioScroll);
        this.add(buttonPanel);
        this.add(doorScroll);
        this.add(pictureIDs);
        
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
    }
        
    protected void updateFloors() throws IOException{
        doorPanel.updateFloors(floorIdx);
        setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(floors[floorIdx].getName()), BorderFactory.createEmptyBorder(5,5,5,5)));
        upButton.setFloorIdx(floorIdx);
        downButton.setFloorIdx(floorIdx);
        if (floors[floorIdx].isUpButtonOn()){
            upButton.pressed();
        } else {
            upButton.unpress();
        }
        
        if (floors[floorIdx].isDownButtonOn()){
            downButton.pressed();
        } else {
            downButton.unpress();
        }
        pictureIDs.update(floors[floorIdx].getPeople());
        repaint();
    }
}
